package com.sgcc.ln.pmos.e_ln_mall.presenter;

import com.sgcc.ln.pmos.common.base.BasePresenter;
import com.sgcc.ln.pmos.common.bean.BaseResponse;
import com.sgcc.ln.pmos.common.bean.ContractPDFBean;
import com.sgcc.ln.pmos.common.bean.PowMonth;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvpNetCallback;
import com.sgcc.ln.pmos.common.utils.UserManager;
import com.sgcc.ln.pmos.e_ln_mall.contract.ContractShowContract;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class ContrsctShowPresenter extends BasePresenter<ContractShowContract.View> implements ContractShowContract.Presenter {

    @Override
    public void geyContractPDF(boolean isfenshi, String type, String combId, String startDate, String endDate, List<PowMonth> timeList) {
        Map<String, Object> params = new HashMap<>();
        params.put("combId", combId);
        String statStr = startDate.substring(0, 7);
        String endStr = endDate.substring(0, 7);
        params.put("startDate", statStr);
        params.put("endDate", endStr);
        params.put("type", type);
        if (timeList != null && timeList.size() != 0) {
            params.put("energyList", changeTimeList(timeList, isfenshi));
        }
        params.put("membersUsersId", UserManager.getUserBean().getMemberId());
        params.put("membersUsersName", UserManager.getUserBean().getMemberName());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_GREATCONTRACT, new MvpNetCallback<ContractPDFBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                if (!isViewAttached()) {
                    return;
                }
                // 失败回调 判断界面是否销毁
                getView().getContractFailde();
            }

            @Override
            public void onResponse(ContractPDFBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().getContractSuccess(response);
            }
        });
    }

    /**
     * 发送短信验证码
     */
    @Override
    public void sendMessageCode() {
        Map<String, Object> params = new HashMap<>();
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_SENDMESSAGE, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                if (!isViewAttached()) {
                    return;
                }
                // 失败回调 判断界面是否销毁
                getView().sendMessageCodeFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().sendMessageCodeSuccess(response);
            }
        });
    }

    /**
     * 发送短信验证码
     */
    @Override
    public void sendMessageCodeNew() {
        Map<String, Object> params = new HashMap<>();
        params.put("flag", "0");
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_SENDMESSAGE_NEW, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                if (!isViewAttached()) {
                    return;
                }
                // 失败回调 判断界面是否销毁
                getView().sendMessageCodeFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().sendMessageCodeSuccess(response);
            }
        });
    }


    @Override
    public void verifyCode(String code) {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("code", code);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_VERIFYCODE, new MvpNetCallback<BaseResponse>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                if (!isViewAttached()) {
                    return;
                }
                // 失败回调 判断界面是否销毁
                getView().verifyCodeFailed();
            }

            @Override
            public void onResponse(BaseResponse response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().verifyCodeSuccess(response);
            }
        });
    }


    private List<List<Double>> changeTimeList(List<PowMonth> timeList, boolean isfenshi) {
        List<List<Double>> energyList = new ArrayList<>();
        for (int i = 0; i < timeList.size(); i++) {
            List<Double> monthList = new ArrayList();
            if (isfenshi) {
                monthList.add(Double.valueOf(timeList.get(i).getREnergy1()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy2()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy3()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy4()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy5()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy6()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy7()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy8()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy9()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy10()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy11()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy12()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy13()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy14()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy15()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy16()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy17()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy18()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy19()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy20()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy21()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy22()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy23()));
                monthList.add(Double.valueOf(timeList.get(i).getREnergy24()));
            } else {
                monthList.add(Double.valueOf(timeList.get(i).getREnergy()));
            }
            energyList.add(monthList);
        }
        return energyList;
    }
}
